{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(79947, 21)"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import datetime\n",
    "df = pd.read_csv(r\"C:\\Users\\KZCF\\Desktop\\cust_analisys\\cust_sample.csv\",sep = ',', encoding='utf-8')\n",
    "df.head()\n",
    "#df[['cust_id','zw_day']]\n",
    "df.dtypes\n",
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(79947, 22)"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['loss_day']=(datetime.datetime(2019,9,1)-pd.to_datetime(df['last_dw_billdate'],format='%Y/%m/%d %H:%M:%S')).dt.days\n",
    "df.replace({\"avg_day\":{\"\\\\N\":\"0\"}},inplace=True)\n",
    "df.replace({\"median_day\":{\"\\\\N\":\"0\"}},inplace=True)\n",
    "df.replace({\"stddev_day\":{\"\\\\N\":\"0\"}},inplace=True)\n",
    "df[\"avg_day\"]=df[\"avg_day\"].astype(\"float\")\n",
    "df[\"median_day\"]=df[\"median_day\"].astype(\"float\")\n",
    "df[\"stddev_day\"]=df[\"stddev_day\"].astype(\"float\")\n",
    "df.dtypes\n",
    "df.shape\n",
    "#df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 35720.31765600694\n",
      "3 45025.177726995535\n",
      "4 52026.04669940091\n",
      "5 54594.74219955776\n",
      "6 58188.26338056477\n",
      "7 60703.39354175585\n",
      "8 64463.521187522536\n",
      "9 67618.32360075184\n"
     ]
    }
   ],
   "source": [
    "from sklearn.cluster import KMeans\n",
    "from sklearn import metrics\n",
    "\n",
    "df2=df[(df['day_num'] >= 5)]\n",
    "#df2=df.loc[(df['day_num'] >= 5)]\n",
    "X=df2[['zw_day', 'day_num', 'total_num', 'total_fee', 'bill_num',\n",
    "       'kz_total_num', 'kz_total_fee', 'kz_bill_num', \n",
    "      'th_total_num', 'th_total_fee', 'th_bill_num',\n",
    "      'avg_day', 'median_day', 'stddev_day', 'loss_day']]\n",
    "    \n",
    "#X.reset_index  无效\n",
    "#X['new_index'] = np.arange(X.shape[0]) #会引发警告\n",
    "#X.index =np.arange(X.shape[0])\n",
    "\n",
    "l_y_kmeans=[]\n",
    "d_y_kmeans={}\n",
    "for i in np.arange(2,10):\n",
    "    kmeans = KMeans(n_clusters=i)\n",
    "    kmeans.fit(X)\n",
    "    y_kmeans = kmeans.labels_\n",
    "    print(i,metrics.calinski_harabaz_score(X, y_kmeans))\n",
    "    l_y_kmeans.append(metrics.calinski_harabaz_score(X, y_kmeans))\n",
    "    d_y_kmeans[i]=metrics.calinski_harabaz_score(X, y_kmeans)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x1cf08300be0>]"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYcAAAD8CAYAAACcjGjIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAIABJREFUeJzt3Xl4VeW59/HvTUKY55kABmQSUCFEQKlWURCsFrTWKirU2tJBrW3tUfGc6/W0tuetvrUOdWgRnFotdQChVkGc6lTAQFAmgQgIGYBgIIQh8/3+sZenqQnJBpKsvZPf57r2lb2f9aydezPkl2c9z1rL3B0REZHKmoVdgIiI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ZWZtgUQLBIZpJ/Ot+JDHF3XcBO81sSNB0PhCTiyi+5Cpi+JBSYAcwzsxaBz8fzicyFxmzzKx78LUfcBn1+Gcczf0cGovxwLXA2uA4PsAd7v5KiDXVpNr7aIRcU2PRA1gY+XlAIvCsuy8Jt6Qa3QQ8Exyq2Upwj5RYFRwPnwh8P+xaauLuK8zsBWA1kcMzGcT+mdIvmlkXoBS4wd331dc3ajJLWUVEJHpN5rCSiIhET+EgIiJVKBxERKQKhYOIiFShcBARkSoUDiIiUoXCQUREqlA4iIhIFf8f6tkgJ7aZq9QAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "n_x=np.arange(2,10)\n",
    "n_y_kmeans=np.array(l_y_kmeans)\n",
    "plt.plot(n_x,n_y_kmeans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "kmeans = KMeans(n_clusters=4)\n",
    "kmeans.fit(X)\n",
    "kmeans.inertia_  #组内平方和\n",
    "y_kmeans = kmeans.labels_\n",
    "#y_kmeans = kmeans.predict(X)  #predict 对新的数据集进行预测\n",
    "df_y_kmeans=pd.DataFrame(y_kmeans)\n",
    "#df_y_kmeans.rename(columns={0:\"y_labels\"},inplace=True)\n",
    "df_y_kmeans.columns = [\"y_labels\"]\n",
    "df_y_kmeans.head()\n",
    "df2.to_csv(r\"C:\\Users\\KZCF\\Desktop\\cust_analisys\\df2.csv\")\n",
    "X.to_csv(r\"C:\\Users\\KZCF\\Desktop\\cust_analisys\\df2_x.csv\")\n",
    "df_y_kmeans.to_csv(r\"C:\\Users\\KZCF\\Desktop\\cust_analisys\\df_y_kmeans2.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "result = pd.merge(X, df_y_kmeans,left_index=True,right_index=True)\n",
    "#result = pd.merge(X, df_y_kmeans,left_on='new_index',right_index=True)\n",
    "result.shape\n",
    "\n",
    "file_name=r\"C:\\Users\\KZCF\\Desktop\\cust_analisys\\cust_sample_result.csv\"\n",
    "if os.path.exists(file_name):\n",
    "    os.remove(file_name)\n",
    "\n",
    "result.to_csv(file_name)\n",
    "df2.to_csv(r\"C:\\Users\\KZCF\\Desktop\\cust_analisys\\df2.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 59586.69351540397\n",
      "3 76813.35435889981\n",
      "4 89347.68989273431\n",
      "5 92833.11145056844\n",
      "6 98962.12934549748\n",
      "7 104353.56156385232\n",
      "8 110973.45265055868\n",
      "9 115129.21178855715\n"
     ]
    }
   ],
   "source": [
    "from sklearn.cluster import KMeans\n",
    "from sklearn import metrics\n",
    "\n",
    "X=df[df.columns.difference(['station_code', 'cust_id','first_dw_billdate', 'last_dw_billdate'])]\n",
    "\n",
    "l_y_kmeans=[]\n",
    "d_y_kmeans={}\n",
    "for i in np.arange(2,10):\n",
    "    kmeans = KMeans(n_clusters=i)\n",
    "    kmeans.fit(X)\n",
    "    y_kmeans = kmeans.labels_\n",
    "    print(i,metrics.calinski_harabaz_score(X, y_kmeans))\n",
    "    l_y_kmeans.append(metrics.calinski_harabaz_score(X, y_kmeans))\n",
    "    d_y_kmeans[i]=metrics.calinski_harabaz_score(X, y_kmeans)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x1cf08c32ba8>]"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAY0AAAD8CAYAAACLrvgBAAAABHNCSVQIC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AGSjVgirtH/VTWGJEEvBV5jSAOeNnd3w+3pGJ9H3gpOOyzDrg95HpOKTjefhnwnbBrKY67zzez14FFRA71fE50fzr8DTNrAuQCd7r7rop6IE25FRGREtPhKRERKTGFhoiIlJhCQ0RESkyhISIiJabQEBGRElNoiIhIiSk0RESkxBQaIiJSYv8fj5T6XT6Nf/cAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "n_x=np.arange(2,10)\n",
    "n_y_kmeans=np.array(l_y_kmeans)\n",
    "plt.plot(n_x,n_y_kmeans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>y_labels</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   y_labels\n",
       "0         0\n",
       "1         0\n",
       "2         0\n",
       "3         0\n",
       "4         0"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kmeans = KMeans(n_clusters=4)\n",
    "kmeans.fit(X)\n",
    "kmeans.inertia_  #组内平方和\n",
    "y_kmeans = kmeans.labels_\n",
    "#y_kmeans = kmeans.predict(X)  #predict 对新的数据集进行预测\n",
    "df_y_kmeans=pd.DataFrame(y_kmeans)\n",
    "#df_y_kmeans.rename(columns={0:\"y_labels\"},inplace=True)\n",
    "df_y_kmeans.columns = [\"y_labels\"]\n",
    "df_y_kmeans.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "result = pd.merge(X, df_y_kmeans,left_index=True,right_index=True)\n",
    "#result = pd.merge(X, df_y_kmeans,left_on='new_index',right_index=True)\n",
    "result.shape\n",
    "\n",
    "file_name=r\"C:\\Users\\KZCF\\Desktop\\cust_analisys\\cust_sample_result.csv\"\n",
    "if os.path.exists(file_name):\n",
    "    os.remove(file_name)\n",
    "\n",
    "result.to_csv(file_name)\n",
    "df2.to_csv(r\"C:\\Users\\KZCF\\Desktop\\cust_analisys\\df2.csv\")\n",
    "#df_y_kmeans.to_csv(r\"C:\\Users\\KZCF\\Desktop\\cust_analisys\\df_y_kmeans.csv\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
